﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ModelPredictPro.WindowForms.AppCodes;
using ModelPredictPro.Predict.SVM;

namespace ModelPredictPro.WindowForms.UserControls {
    public partial class UCPredictTask : SVMUserControl, ISVMTaskHost {
        public UCPredictTask() {
            InitializeComponent();
        }

        private void UCPredictTask_Load(object sender, EventArgs e) {
        }

        private void btnAddTask_Click(object sender, EventArgs e) {
            bool validate = true;
            validate = txtTrainDatasetFile.CheckFileExist();
            validate = txtTestDatasetFile.CheckFileExist();
            validate = txtResultOutputFile.CheckFileDirectoryExist() && validate;
            double c, gamma;
            c = gamma = double.NaN;
            double.TryParse(txtParamC.Text, out c);
            double.TryParse(txtParamGamma.Text, out gamma);

            bool d = double.IsNaN(c) || c <= 0;
            validate = validate && !d;
            if (d)
                errorProvider1.SetError(txtParamC, "Please input a double value (>=0) for param C.");
            else
                errorProvider1.SetError(txtParamC, "");

            d = double.IsNaN(gamma) || gamma <= 0;
            validate = validate && !d;
            if(d)
                errorProvider1.SetError(txtParamGamma, "Please input a double value (>=0) for param C.");
            else
                errorProvider1.SetError(txtParamGamma, "");

            if (validate)
                AddTask(CreateTask());
        }

        #region abstract field

        public override ITask CreateTask() {
            ITask task = null;
            SVMFactory factory = UCSVMSetting.SingleInstance.CreateSVMFactory();
            var parma = factory.CreatePredictParam();
            parma.C = double.Parse(txtParamC.Text);
            parma.Gamma = double.Parse(txtParamGamma.Text);
            task = new PredictTask(parma, TrainSetFile, TestSetFile, ResultOutputFile);
            return task;      
        }

        public override string TrainSetFile {
            get {
                return txtTrainDatasetFile.Text;
            }
            set {
                txtTrainDatasetFile.Text = value;
            }
        }

        public override string TestSetFile {
            get {
                return txtTestDatasetFile.Text;
            }
            set {
                txtTestDatasetFile.Text = value;
            }
        }

        public override string ResultOutputFile {
            get {
                return txtResultOutputFile.Text;
            }
            set {
                txtResultOutputFile.Text = value;
            }
        }

        #endregion

        private void btnBulkAdd_Click(object sender, EventArgs e) {
            string[] files;
            bool batch = txtTrainDatasetFile.CheckMultiFileExist(out files);
            if (!batch) return;
            batch = txtTestDatasetFile.CheckMultiFileTemplate() && batch;
            batch = txtResultOutputFile.CheckMultiFileTemplate() && batch;
            if (!batch) return;

            if (MessageBox.Show(string.Format("Add {0} tasks, continue?", files.Length), "Query", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) {

                SVMFactory factory = UCSVMSetting.SingleInstance.CreateSVMFactory();
                var parma = factory.CreatePredictParam();

                foreach (string item in files) {
                    string testeDatasetFile = txtTestDatasetFile.GetRealFilePath(item);
                    string resultOutputFile = txtResultOutputFile.GetRealFilePath(item);
                    AddTask(new PredictTask(parma, item, testeDatasetFile, resultOutputFile));
                }
            }
        }

    }
}
